package cn.xiaomubiao.oschine.fragment.myinfofragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.xiaomubiao.oschine.R;
import cn.xiaomubiao.oschine.adapter.myinfo.MyInfoBaseViewPagerAdapter;
import cn.xiaomubiao.oschine.fragment.myinfofragment.collection.MyInfoBean;

/**
 * @author
 * @version 1.0
 * @date 2017/4/2
 */

public abstract class MyInfoBaseTabViewFragment extends Fragment {

    @BindView(R.id.info_base_tab)
    TabLayout mInfoBaseTab;
    @BindView(R.id.info_base_vp)
    ViewPager mInfoBaseVp;
    Unbinder unbinder;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.view_myinfo_base_tabvp_fragment, container, false);
        unbinder = ButterKnife.bind(this, view);
       
        

        initView();
        return view;
    }

    private void initView() {
        mInfoBaseTab.setupWithViewPager(mInfoBaseVp);
        MyInfoBaseViewPagerAdapter adapter = new MyInfoBaseViewPagerAdapter(getActivity(),getChildFragmentManager());
        mInfoBaseVp.setAdapter(adapter);
        updateAdapterDatas(adapter);
        mInfoBaseTab.setTabMode(TabLayout.MODE_FIXED);

        Bundle arguments = getArguments();
        String tag = arguments.getString("tag");
        if ("粉丝".equals(tag)){
            mInfoBaseVp.setCurrentItem(1);
        }
    }

    public  void updateAdapterDatas(MyInfoBaseViewPagerAdapter adapter) {
        int[] array = updateData();
        if (array.length == 0){
            return;
        }
        int idTitle = array[0];
        int idClassName = array[1];
        int idArgs = array[2];

        String[] arrayTitle = getResources().getStringArray(idTitle);
        String[] arrayClzz = getResources().getStringArray(idClassName);
        String[] arrayArgs = getResources().getStringArray(idArgs);
        
        if ((arrayTitle.length != arrayClzz.length )||(arrayArgs.length != arrayClzz.length )){
            Toast.makeText(getActivity(), "String Array数据不一致", Toast.LENGTH_SHORT).show();
            return;
        }
        List<MyInfoBean> datas = new ArrayList<>();
        for (int i = 0; i < arrayTitle.length; i++) {
            MyInfoBean bean = new MyInfoBean(arrayClzz[i],arrayTitle[i],arrayArgs[i]);
            datas.add(bean);
        }
        adapter.updateDatas(datas);
    }
    
    public abstract  int[]  updateData();
        

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
